define(['moment'], function (moment) {
    return ['$scope', '$filter', '$mdDialog', 'config', 'MMWService', '$calculater', '$gallery', '$timeout',
        function ($scope, $filter, $mdDialog, config, $MMWService, $calculater, $gallery, $timeout) {
            $scope.init = function () {
                $scope.condition = {
                    plot_no: '',
                    op_no: ''
                }
                $scope.selectAll = false;
                $scope.datalist = [];
            };

            $scope.back = function () {
                $scope.$root.hideMenu();
                $scope.$root.changeProgram('home', true);
            };

            // 确认
            $scope.confirm = function () {
                let selected = $scope.datalist.filter(d => d.isSelect);
                if (selected.length == 0) {
                    $scope.$root.showAlert($filter('translate')('common.msg.plz_select_one'));
                    return;
                }
                // 检查小数位数
                var flag = true;
                for (var i = 0; i < selected.length; i++){
                    var tempUseQty = selected[i].use_qty + '';
                    if (tempUseQty.indexOf(".") != -1) {
                        var qtyIndex = tempUseQty.indexOf(".") + 1;
                        var qtyCount = tempUseQty.length - qtyIndex;
                        if (qtyCount > 6) {
                            flag = false;
                            break;
                        }
                    }
                }
                if(!flag){
                    $scope.$root.showAlert($filter('translate')('B1601.msg.max_six_decimal'));
                    return;
                }
                var content = {
                    list: selected
                }
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_MAT.material_deduct_process',
                    content: content,
                    success: function (data) {
                        $scope.$root.showToast($filter('translate')('common.msg.success'));
                        $scope.search();
                    },
                    error: function (data) {
                        $scope.$root.showAlert(data.Message);
                    },
                });

            };

            $scope.input_use_qty = function (event, item) {
                var target = event.currentTarget;
                angular.element(target).addClass('is-focus');
                $calculater.open(function (num, dialogNum) {
                    angular.element(target).removeClass('is-focus');
                    item.use_qty = num;
                    dialogNum.hide();
                },
                function (dialogNum) {
                    angular.element(target).removeClass('is-focus');
                    dialogNum.hide();
                },
                {
                    defaultNum: item.use_qty,
                    decimal: true
                });
            }

            // 全选
            $scope.selectAllData = function () {
                $scope.selectAll = !$scope.selectAll;
                $scope.datalist.forEach(function (item) {
                    item.isSelect = $scope.selectAll;
                });
                if ($scope.datalist.length == 0) {
                    $scope.selectAll = false;
                }
            };

            // 选择一行
            $scope.select = function (event, item) {
                event.stopPropagation();
                item.isSelect = !item.isSelect

                var checkarr = []
                $scope.datalist.forEach(function (data) {
                    if (data.isSelect) {
                        checkarr.push(data)
                    }
                })
                if (checkarr.length === $scope.datalist.length) {
                    $scope.selectAll = true;
                } else {
                    $scope.selectAll = false;
                }
            };

            //查詢 - 確認
            $scope.search = function () {
                var content = {
                    plot_no: $scope.condition.plot_no,
                    op_no: $scope.condition.op_no
                }
                $MMWService.sendToServer({
                    uri: 'ServicesSTD.Module_MAT.material_deduct_get',
                    content: content,
                    success: function (data) {
                        if (data.list.length == 0) {
                            $scope.datalist = [];
                            $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                        } else {
                            $scope.datalist = data.list;
                        }
                    }
                });
            };

        }
    ];
});
